<?php
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// DLMan by Shedd Technologies International		  				//
// http://www.dlman.com | info@dlman.com							//
// Copyright 2003 by STI, All rights reserved.						//
// ---------------------------------------------------------------- //
// Usage of this software is governed by the terms of GPL. 	    	//
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

require_once("global.php");
ob_start();//start content buffer
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	if(isset($larea)&&$larea==""){
		?>
		<form action="<?php print $PHP_SELF; ?>" method="post">
		<P><FONT face=Verdana><b>View Logs</b></FONT></P>
		Search Download/Licensing Logs by
		<select name="search_by" size="1" class="prefinput">
			<option value="user">Username</option>
			<option value="cf">Custom Field Data</option>
			<option value="email">Email</option>
			<option value="date">Date</option>
			<option value="file">File</option>
		</select><br><br>
		<i>Target Value</i><br>
		Username:&nbsp;<input type="text" name="username" size="25"><br>
		Custom Field Data:&nbsp;<input type="text" name="cf" size="25">&nbsp;(License: 
		<!--LICENSE SELECT BOX-->
		<select name="cf_lic" size="1" class="prefinput">
			<?php
				$s="SELECT * FROM ".$config->dt['license'];
				$r=mysql_query($s);
				while($v=mysql_fetch_array($r)){
					?>
					<option value="<?php print $v['id']; ?>"><?php print $v['title']; ?></option>
					<?php
				}//end while
			?>
			<option value="*">*</option>
		</select>
		<!--end LICENSE SELECT BOX-->
		<!--CUSTOM FIELD SELECT BOX-->
		&amp; Custom Field: 
		<select name="cf_id" size="1" class="prefinput">
			<?php
			for($i=1;$i<6;$i++){
				print '<option value="'.$i.'">'.$i.'</option>';
			}
			?>
			<option value="*">*</option>
		</select>
		<!--end CUSTOM FIELD SELECT BOX-->
		)<br>
		Email:&nbsp;<input type="text" name="email" size="25"><br>
		Date of Download:&nbsp;
		<select name="date_month" size="1" class="prefinput">
			<?php
			$month_array=array('January','February','March','April','May','June','July','August','September','October','November','December');
			for($i=1;$i<13;$i++){
				print '<option value="'.$i.'">'.$month_array[$i-1].'</option>';
			}
			?>
		</select> (Month)
		<select name="date_day" size="1" class="prefinput">
			<?php
			for($i=1;$i<31;$i++){
				print '<option value="'.$i.'">'.$i.'</option>';
			}
			?>
		</select> (Day)
		<select name="date_year" size="1" class="prefinput">
			<?php
			for($i=2003;$i<=(date('Y')+7);$i++){
				print '<option value="'.$i.'">'.$i.'</option>';
			}
			?>
		</select> (Year)<br>
		File:&nbsp;<select name="file" size="1" class="prefinput">
			<?php
				$s="SELECT * FROM ".$config->dt['files'];
				$r=mysql_query($s);
				while($v=mysql_fetch_array($r)){
					?>
					<option value="<?php print $v['id']; ?>"><?php print $v['name']; ?></option>
					<?php
				}//end while
			?>
		</select><br>
		<input name="submit" type="submit" value="Search">&nbsp;&nbsp;<input name="submit" type="submit" value="View All">
		<br><i>To export log data, please select a range above and click to proceed.</i>
		<input type="hidden" name="larea" value="do">
		</form>
		<?php
	}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	elseif($larea=="do"){
		?>
			<script language="JavaScript" type="text/javascript">
			function check(checkbox){
				for (i=0;i<theForm.elements.length;i++){
				 	if(document.theForm.elements[i].type == "checkbox"){
						theForm.elements[i].checked = checkbox.checked; 
					}
				}
			} 
			
			//show status bar messages
			function statusbar(statusmsg){
				window.status=statusmsg
				return true
			}
			//end show status bar message function
			</script>
			<form action="<?php print $PHP_SELF; ?>" method="post" name="theForm">
			<input type="hidden" name="larea" value="export">
			<table align="center" cellspacing="0" cellpadding="2" border="0">
			<tr>
			    <td align="center"><INPUT TYPE="CHECKBOX" onClick="check(this)" onMouseover="return statusbar('Select All')" onMouseout="return statusbar('')"></td>
			    <td align="center"><b>Name</b></td>
			    <td align="center"><b>Product</b></td>
			    <td align="center"><b>Date</b></td>
				<td align="center"><b>Transaction</b></td>
			</tr>
			<tr><td colspan="5" bgcolor="black"></td></tr>
		<?php
		//search
		if($submit!="View All"){
			//search by username
			if($search_by=="user"){
				$s="SELECT ".$config->field['userid']." FROM ".$config->dt['user']." WHERE ".$config->field['username']."='$username'";
				$r=@mysql_query($s);
				$v=@mysql_fetch_array($r);
				$sql="SELECT * FROM ".$config->dt['log']." WHERE user_id='".$v[$config->field['userid']]."'";
				$result=@mysql_query($sql);
			}
			////////////////////////////////////////////////////////////////////////
			//search by custom field data
			elseif($search_by=="cf"){
				$s="SELECT txn_id FROM ".$config->dt['license_data']." as L,".$config->dt['files']." as F";
				//handle wildcard field id
				if($cf_id=="*"){
					$s.=" WHERE";
					for($x=1;$x<6;$x++){
						$field="L.data_".$x;
						$s.=" $field LIKE '%$cf%'";
						if($x<5) $s.=" OR";
					}//end for
				}//end wildcard active
				//no wildcards
				else{
					$field="L.data_".$cf_id;
					$s.=" WHERE $field LIKE '%$cf%'";
				}//end no wildcards
				
				//specific license?
				if($cf_lic!="*"){
					$s.=" AND F.license='$cf_lic'";
				}
				//complete join
				$s.=" AND L.file_id=F.id";
				
				$r=@mysql_query($s);
				$v=@mysql_fetch_array($r);
				$sql="SELECT * FROM ".$config->dt['log']." WHERE txn_id='".$v['txn_id']."'";
				$result=@mysql_query($sql);
			}
			////////////////////////////////////////////////////////////////////////
			//search by email
			elseif($search_by=="email"){
				$s="SELECT ".$config->field['userid']." FROM ".$config->dt['user']." WHERE ".$config->field['email']."='$email'";
				$r=@mysql_query($s);
				$v=@mysql_fetch_array($r);
				$sql="SELECT * FROM ".$config->dt['log']." WHERE user_id='".$v[$config->field['userid']]."'";
				$result=@mysql_query($sql);
			}
			////////////////////////////////////////////////////////////////////////
			//search by date
			elseif($search_by=="date"){
				$target_date=mktime(0,0,0,$date_month,$date_day,$date_year);
				$target_date_bound=mktime(0,0,0,$date_month,$date_day+1,$date_year);
				$sql="SELECT * FROM ".$config->dt['log']." WHERE timestamp>='".$target_date."' AND timestamp<'".$target_date_bound."'";
				$result=@mysql_query($sql);
			}
			////////////////////////////////////////////////////////////////////////
			//search by file
			elseif($search_by=="file"){
				$sql="SELECT * FROM ".$config->dt['log']." WHERE file_id='".$file."'";
				$result=@mysql_query($sql);
			}
			////////////////////////////////////////////////////////////////////////
		}//end search
		//view all
		else{
			$sql="SELECT * FROM ".$config->dt['log'];
			$result=@mysql_query($sql);
		}//end view all
		
		//display result set
		if(@mysql_num_rows($result)==0){
			print '<tr><td colspan="4"><br></td></tr><tr><td align="center" colspan="4"><b>No Matching Records Found</b></td></tr><tr><td colspan="4"><br></td></tr>';
		}
		else{
			while($value=@mysql_fetch_array($result)){
				row($value);
			}
		}
		?>
		<tr><td colspan="5" bgcolor="black"></td></tr>
		<tr>
			<td colspan="5">
				<input type="submit" value="Export"> Selected Records by 
				<?php
				for($i=1;$i<6;$i++){
					?>
					<select name="c<?php print $i; ?>" size="1" class="prefinput">
						<option value="none">-</option>
						<option value="<?php print $config->field['username']; ?>">Name</option>
						<option value="<?php print $config->field['email']; ?>">Email</option>
						<option value="pname_9567">Product</option>
						<option value="time_9567">Time</option>
						<option value="date_9567">Date</option>
					</select><?php 
					if($i<5) print "&nbsp;&nbsp;";
				}//end for
				?>
				<br>
				Seperate Columns by <input type="text" name="coldelim" size="5"> , 
				Seperate Rows by <input type="text" name="rowdelim" size="5"><br>
				Time Format <input type="text" name="timeform" size="7">&nbsp;&nbsp; 
				Date Format <input type="text" name="dateform" size="7"> (see <a href="http://www.php.net/manual/en/function.date.php" target="_blank">PHP.net</a> for format)
			</td>
		</tr>
		</table>
		</form>
		<?php
		print "<br><div align='center'><a href='$PHP_SELF?larea='>Back to View & Export Logs</a></div>";
	}//end do
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
	elseif($larea=="export"){
		print "<div align='center'>";
		//count columns set
		$c=0;
		for($i=1;$i<6;$i++){
			$el="c".$i;
			if($$el!="none"){
				$c++;
			}
		}
		
		foreach($record as $num){
			//join log.uid=users.uid & log.file_id & files.id
			$query="SELECT U.".$config->field['username'].",U.".$config->field['email'].",F.name,L.timestamp FROM ".$config->dt['log']." as L,".$config->dt['user']." as U,".$config->dt['files']." as F WHERE L.user_id=U.".$config->field['userid']." AND L.file_id=F.id AND txn_id='$num'";
			$result=mysql_query($query);
			if(!$result){
				print "Error in exporting data for record $num!";
				print mysql_error();
			}
			else{
				$x=1;
				$value=mysql_fetch_array($result);
				for($i=1;$i<=5;$i++){
					//output c$i
					$el="c".$i;
					if($$el!="none"){
						//username
						if($$el==$config->field['username']){
							print $value[$config->field['username']];
						}
						//email
						elseif($$el==$config->field['email']){
							print $value[$config->field['email']];
						}
						//product
						elseif($$el=="pname_9567"){
							print $value['name'];
						}
						//time
						elseif($$el=="time_9567"){
							print date($timeform,$value['timestamp']);
						}
						//date
						elseif($$el=="date_9567"){
							print date($dateform,$value['timestamp']);
						}
					}//end not none
					//END COLUMN
					if($x+1<=$c)print $coldelim;
					$x++;
				}//end for
			}//end else: query success
			print $rowdelim;//END ROW
		}//end foreach
		print "<br><br><a href='$PHP_SELF?larea='>Back to View & Export Logs</a></div>";
	}//end export
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
$content=ob_get_contents();//store content to global buffer var
ob_end_clean();
return $content;
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//FUNCTIONS
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
/*
	Output the transaction log data into a row.
*/
function row($value){
	global $config;
	//get username from id
	$s="SELECT ".$config->field['username']." FROM ".$config->dt['user']." WHERE ".$config->field['userid']."='".$value['user_id']."'";
	$r=@mysql_query($s);
	$v=@mysql_fetch_array($r);
	//get product name from id
	$sql="SELECT name FROM ".$config->dt['files']." WHERE id='".$value['file_id']."'";
	$result=mysql_query($sql);
	$v2=mysql_fetch_array($result);
	?>
		<tr>
		    <td align="center"><input type="checkbox" name="record[<?php print $value['record_id']; ?>]" value="<?php print $value['txn_id']; ?>"></td>
		    <td align="center"><a href="#" onclick="javascript:window.open('license.php?record=<?php print $value['record_id']; ?>&<?php print session_name()."=".session_id()?>', 'newwindow', config='height=400, width=400, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no')" onMouseover="return statusbar('View License Data')" onMouseout="return statusbar('')"><?php print $v[$config->field['username']]; ?></a></td>
		    <td align="center"><?php print $v2['name']; ?></td>
		    <td align="center"><?php print date("F j, Y (H:i:s)",$value['timestamp']); ?></td>
			<td align="center"><?php print $value['record_id']; ?></td>
		</tr>
	<?php
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
?>
